package com.thb.pay.system.mapper;

import com.thb.pay.db.mapper.SuperBaseMapper;
import com.thb.pay.system.entity.SysResource;
import com.thb.pay.system.entity.SysRoleResource;
import com.thb.pay.system.entity.SysUserResource;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 * 系统-资源表 Mapper 接口
 * </p>
 *
 * @author lucky
 * @since 2019-07-21
 */
@Mapper
public interface SysResourceMapper extends SuperBaseMapper<SysResource> {

    @Select(value = " SELECT DISTINCT (s.id), s.* " +
            " FROM " + SysResource.TABLE_NAME + " AS s " +
            " JOIN " + SysUserResource.TABLE_NAME + " AS p ON p.resource_id = s.id " +
            " WHERE 1 = 1 " +
            " AND p.user_id = #{userId} " +
            " AND s.status = 1 " +
            " AND s.del_flag = 0 " +
            " ORDER BY s.order_num ASC ")
    List<SysResource> selectListByUserId(@Param("userId") Long userId);


    @Select(value = "<script>" +
            " SELECT DISTINCT (s.id), s.* " +
            " FROM " + SysResource.TABLE_NAME + " AS s " +
            " JOIN " + SysRoleResource.TABLE_NAME + " AS p ON p.resource_id = s.id " +
            " WHERE 1 = 1 " +
            " AND p.role_id in  " +
            "<foreach collection=\"list\" item=\"item\" index=\"index\" open=\"(\" separator=\",\" close=\")\">\n" +
            " #{item}\n" +
            "</foreach> " +
            " AND s.status = 1 " +
            " AND s.del_flag = 0 " +
            " ORDER BY s.order_num ASC " +
            "</script>")
    List<SysResource> selectListByRoleIds(List<Long> roleIds);
}
